using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using Gencode.Common.SQL;

namespace Personalplaner
{
    class DJPGenerator
    {
        List<IPersonRow> rows = new List<IPersonRow>();

        public DJPGenerator(int prsID, int yearid, string dienstIDs, bool mitWeekend, bool mitFeiertage, bool alles)
        {
            addRow(prsID, yearid, dienstIDs, mitWeekend, mitFeiertage, alles);
        }

        private void addRow(int prsID, int yearid, string dienstIDs, bool mitWeekend, bool mitFeiertage, bool alles)
        {
            PAbteilungRow abtRow = null;
            PersonJPRow prsRow = null;

            DataView dv = getRowDV(prsID, yearid);
            if (dv.Count > 0)
            {
                //add first abteilung
                abtRow = new PAbteilungRow();
                abtRow.AbteilungID = Int16.Parse(dv[0].Row[0].ToString());
                abtRow.AbteilungName = dv[0].Row[1].ToString();
                abtRow.SetData();

                rows.Add(abtRow);

                int oldAbteilung = Int16.Parse(dv[0].Row[0].ToString());
                while (dv.Count > 0)
                {
                    if (oldAbteilung == Int16.Parse(dv[0].Row[0].ToString()))
                    {
                        //ad plan row
                        prsRow = new PersonJPRow
                        {
                            AbteilungID = Int16.Parse(dv[0].Row[0].ToString()),
                            AbteilungName = dv[0].Row[1].ToString(),
                            PlanID = Int16.Parse(dv[0].Row[2].ToString()),
                            PlanName = dv[0].Row[3].ToString(),
                            MonatName = dv[0].Row[4].ToString(),
                            PrsID = prsID,
                            DienstIDs = dienstIDs,
                            Weekend = mitWeekend,
                            Feiertage = mitFeiertage,
                            Alles = alles
                        };

                        prsRow.SetData();

                        rows.Add(prsRow);
                        oldAbteilung = Int16.Parse(dv[0].Row[0].ToString());
                        dv.Delete(0);
                    }
                    else
                    {
                        //add abteilung
                        abtRow = new PAbteilungRow();
                        abtRow.AbteilungID = Int16.Parse(dv[0].Row[0].ToString());
                        abtRow.AbteilungName = dv[0].Row[1].ToString();
                        abtRow.SetData();

                        rows.Add(abtRow);
                        oldAbteilung = Int16.Parse(dv[0].Row[0].ToString());
                    }
                }

            }
        }

        private DataView getRowDV(int prsID, int yearid)
        {
			Querry stp = new Querry();
            stp.SetSTPName("get_personjprow");
            stp.AddParameter("@prsid", prsID);
            stp.AddParameter("@jahrid", yearid);
			
			return stp.ExecuteForDataView();
        }

        #region Properties
        internal List<IPersonRow> Row
        {
            get { return rows; }
            set { rows = value; }
        }

        #endregion
    }
}
